package com.ideaaedi.zoo.commonbase.util;

import javax.annotation.Nonnull;
import java.lang.reflect.Method;

/**
 * 唯一标识工具类
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
public final class UidUtil {
    
    /**
     * 获取方法唯一标识
     *
     * @param method 方法
     *
     * @return 方法唯一标识
     */
    @Nonnull
    public static String buildMethodUid(@Nonnull Method method) {
        StringBuilder sb = new StringBuilder(128);
        Class<?>[] parameterTypes = method.getParameterTypes();
        sb.append(method.getDeclaringClass().getName())
                .append("#")
                .append(method.getName())
                .append("(");
        for (int i = 0; i < parameterTypes.length; i++) {
            Class<?> argClass = parameterTypes[i];
            String argClassName = argClass.getName();
            String argClassSimpleName = argClass.getSimpleName();
            if (i > 0) {
                sb.append(",");
            }
            // @clnhc即：@ class long name hash code
            sb.append(argClassSimpleName).append("@clnhc").append(argClassName.hashCode());
        }
        sb.append(")");
        return sb.toString();
    }
}
